//
//  HMWelcomeViewViewController.swift
//  sinaweibo
//
//  Created by lst on 16/5/25.
//  Copyright © 2016年 lst. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class HMWelcomeViewController: UIViewController {
    
    
    override func loadView() {
        view = backView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    //设置UI界面
    private func setupUI() {
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        //设置约束
        //在初始时候就设置约束  不要在viewDidLayoutSubviews中设置约束
        //系统是根据提前指定好的约束 在viewDidLayoutSubviews方法中根据约束修改frame
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            //固定图片的大小
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        //SDWebImage 框架在3.7.6版本图片会根据屏幕的分辨率进行缩放
        iconView.sd_setImageWithURL(HMUserAccountViewModel.sharedAccountViewModel.headURL)
//        let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(HMUserAccountViewModel.sharedAccountViewModel.userAccount?.avatar_large)
//        print(image.scale)
       
        //设置圆角
        iconView.cornerRadius = 45
        //是指nameLabel  透明度
        nameLabel.alpha = 0
        
    }
    
    
    //动画效果建议放在viewDidAppear阶段执行
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation() {
        //弹簧动画效果
        //usingSpringWithDamping: 阻尼系数 0 ~ 1 值越小 效果越弹
        //initialSpringVelocity : 加速度 值越大 速度越快
        //OC:位移枚举 表示多个动画选项 '|' swift: 使用数组数组
        let offset = -ScreenHeight + 160
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: {
            //修改约束
            //            self.iconView.snp_makeConstraints(closure: { (make) in
            //                make.bottom.equalTo(-400)
            //            })
            //取消之前的所有约束 在重写添加约束
            //self.iconView.snp_remakeConstraints(closure: <#T##(make: ConstraintMaker) -> Void#>)
            //更新约束
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(offset)
            })
            
            //提前刷新父视图
            self.view.layoutIfNeeded()
        }) { (_) in
            UIView.animateWithDuration(0.5, animations: {
                self.nameLabel.alpha = 1
                }, completion: { (_) in
                    print("👌")
                    //发出切换根式图控制器的通知
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
            })
        }
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    //懒加载子视图
    //背景视图
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    ///用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    ///用户名称文案
    private lazy var nameLabel: UILabel = UILabel(title: HMUserAccountViewModel.sharedAccountViewModel.userAccount?.name ?? "", font: 18, textColor: UIColor.darkGrayColor())
    //        let l = UILabel()
//        print(HMUserAccountViewModel.sharedAccountViewModel.userAccount?.name)
//        l.text = HMUserAccountViewModel.sharedAccountViewModel.userAccount?.name
//        l.font = UIFont.systemFontOfSize(18)
//        l.textColor = UIColor.darkGrayColor()
//        l.alpha = 0
//        //自适应大小
//        l.sizeToFit()
//        return l
//    }()
    
}
